package com.longcai.piaojia.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.longcai.piaojia.R;
import com.longcai.piaojia.beans.CityNames;
import com.zcx.helper.bound.BoundViewHelper;
import com.zcx.helper.scale.ScaleScreenHelperFactory;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/8/23.
 */
public class ChooseCityListAdapter extends BaseAdapter {


    private Context context;
    private List<CityNames> list;
    private ViewHolder viewHolder;
    private int prePosition = -1;
    private Map<String, Boolean> map = new HashMap<>();

    public ChooseCityListAdapter(Context context, List<CityNames> list) {
        this.context = context;
        this.list = list;

        for(int i=0;i<list.size();i++){

            map.put(list.get(i).getName(), false);

        }

        for(int i=0;i<list.size();i++){

            if(list.get(i).getName().length()==1){

                map.put(list.get(i+1).getName(), true);

            }

        }

    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public boolean isEnabled(int position) {
        // TODO Auto-generated method stub
        if (list.get(position).getName().length() == 1)// 如果是字母索引
            return false;// 表示不能点击
        return super.isEnabled(position);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        String item = list.get(position).getName();
        viewHolder = new ViewHolder();
        if (item.length() == 1) {
            ScaleScreenHelperFactory.getInstance().loadView((ViewGroup) (convertView = LayoutInflater.from(context).inflate(R.layout.item_choose_city_pinyin_list, null)));
            viewHolder.indexTv = (TextView) convertView.findViewById(R.id.item_choose_city_pinyin_tv);
            viewHolder.item_choose_city_pinyin_list_line_tv = (TextView) convertView.findViewById(R.id.item_choose_city_pinyin_list_line_tv);

            if (position == 0) {
                viewHolder.item_choose_city_pinyin_list_line_tv.setVisibility(View.GONE);
            }else{
                viewHolder.item_choose_city_pinyin_list_line_tv.setVisibility(View.VISIBLE);
            }

        } else {

            ScaleScreenHelperFactory.getInstance().loadView((ViewGroup) (convertView = LayoutInflater.from(context).inflate(R.layout.item_choose_city_name_list, null)));
            viewHolder.itemTv = (TextView) convertView.findViewById(R.id.item_choose_city_name_list_tv);
            viewHolder.item_choose_city_name_list_line_tv = (TextView) convertView.findViewById(R.id.item_choose_city_name_list_line_tv);
        }
        if (item.length() == 1) {
            prePosition = position;
            viewHolder.indexTv.setText(list.get(position).getName());
        } else {
            if(map.get(list.get(position).getName())){

                viewHolder.item_choose_city_name_list_line_tv.setVisibility(View.GONE);

            }
            viewHolder.itemTv.setText(list.get(position).getName().replace("市", ""));
        }
        return convertView;
    }

    private class ViewHolder {
        private TextView indexTv;
        private TextView itemTv;
        private TextView item_choose_city_name_list_line_tv;
        private TextView item_choose_city_pinyin_list_line_tv;
    }

}
